<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit campaigns.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Campaigns_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["campaign"] = new Campaign($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["campaign"]->id);

    // To one relationships
        
    $clients = new Client();
    $data["clients"] = $clients->all_to_asoc_array("id","name");
    $data["clients_selected"] = $data["campaign"]->client->id;
        
    $templates = new Template();
    $data["templates"] = $templates->all_to_asoc_array("id","name");
    $data["templates_selected"] = $data["campaign"]->template->id;
    
    // To many relationships
    
    $sends = new Send();
    $data["sends"] = $sends->all_to_asoc_array("id","read");
    $data["sends_selected"] = $data["campaign"]->get_relationship_ids("send");
    
    $this->smarty_parser->view('campaign_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["campaign"] = new Campaign($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->subject = $this->input->post("subject");
      if ($data["campaign"]->subject == "") $data["campaign"]->subject = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->subject->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(subject)) $data["campaign"]->subject = $fileupload.clean_file_name(subject);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->created = $this->input->post("created");
      if ($data["campaign"]->created == "") $data["campaign"]->created = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->created->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(created)) $data["campaign"]->created = $fileupload.clean_file_name(created);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->filters = $this->input->post("filters");
      if ($data["campaign"]->filters == "") $data["campaign"]->filters = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->filters->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(filters)) $data["campaign"]->filters = $fileupload.clean_file_name(filters);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->from = $this->input->post("from");
      if ($data["campaign"]->from == "") $data["campaign"]->from = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->from->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(from)) $data["campaign"]->from = $fileupload.clean_file_name(from);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->name = $this->input->post("name");
      if ($data["campaign"]->name == "") $data["campaign"]->name = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->name->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(name)) $data["campaign"]->name = $fileupload.clean_file_name(name);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->body = $this->input->post("body");
      if ($data["campaign"]->body == "") $data["campaign"]->body = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->body->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(body)) $data["campaign"]->body = $fileupload.clean_file_name(body);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->sent = $this->input->post("sent");
      if ($data["campaign"]->sent == "") $data["campaign"]->sent = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->sent->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(sent)) $data["campaign"]->sent = $fileupload.clean_file_name(sent);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->tplbody = $this->input->post("tplbody");
      if ($data["campaign"]->tplbody == "") $data["campaign"]->tplbody = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->tplbody->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(tplbody)) $data["campaign"]->tplbody = $fileupload.clean_file_name(tplbody);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["campaign"]->shipment = $this->input->post("shipment");
      if ($data["campaign"]->shipment == "") $data["campaign"]->shipment = NULL;
      
      // files for upload
      /*
      if ($data["campaign"]->shipment->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(shipment)) $data["campaign"]->shipment = $fileupload.clean_file_name(shipment);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["campaign"]->save())
      $data["error"] .= $data["campaign"]->error->string;

    //saving to one relationships
    
    $client = new Client($this->input->post("client"));
    if (! $data["campaign"]->save(array($client)))
      $data["error"] .= $data["campaign"]->error->string;
    
    $template = new Template($this->input->post("template"));
    if (! $data["campaign"]->save(array($template)))
      $data["error"] .= $data["campaign"]->error->string;
    
    //to many relationships
            
    redirect(base_url()."campaigns/edit/".$data["campaign"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["campaign"] = new Campaign($id);

    //delete
    if(! $data["campaign"]->delete())
      $data["error"] .= $data["campaign"]->error->string;

    redirect(base_url()."campaigns/edit/");
  } // end of method delete($id)

    
} // class Campaigns_protoweb

/* End of file campaigns_protoweb.php */
/* Location: ./system/application/controllers/campaigns_protoweb.php */
